package homework.homework_9.ktv;

import homework.homework_8.ktv.KTV;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * @author 朱
 * @date 2019041413:19
 */
public class KTVDaoImp implements KTVDao{
    static ArrayList<Ktv> arrayList=new ArrayList<Ktv>();
    static Object object=arrayList;
    @Override
    public void regist(Ktv ktv) {
       arrayList.add(ktv);
        System.out.println(object);
        BufferedWriter writer=null;
        try {
            writer=new BufferedWriter(new FileWriter("ktv.txt"));
            for (int i = 0; i <arrayList.size() ; i++) {
                writer.write(arrayList.get(i).getName());
                writer.newLine();
                writer.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void forward(String name) {
        BufferedReader reader=null;
        try {
            reader=new BufferedReader(new FileReader("ktv.txt"));
            String str=reader.readLine();
            Ktv ktv=new Ktv(str);
            for (int i = 1; i <arrayList.size() ; i++) {
                if (arrayList.get(i).getName().equals(str)){
                    arrayList.remove(ktv);
                    arrayList.add(arrayList.size()-1,ktv);
                }
            }
            System.out.println(object);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void top(String name) {
    Ktv ktv=new Ktv(name);
        for (int i = 0; i <arrayList.size() ; i++) {
            if (arrayList.get(i).getName().equals(name)){
                arrayList.remove(ktv);
                arrayList.add(0,ktv);
            }
        }
        System.out.println(object);
    }
}
